/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package beans;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author machine
 */
@Stateless
public class SeriespeliculasFacade extends AbstractFacade<Seriespeliculas> implements SeriespeliculasFacadeLocal {
    @PersistenceContext(unitName = "videosJEE-ejbPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public SeriespeliculasFacade() {
        super(Seriespeliculas.class);
    }

    @Override
    public Integer consultarCantidad(Integer idseries) {
         Long rowCnt = (Long) em.createNativeQuery("SELECT count(1) FROM seriespeliculas v where v.idseries = "+idseries+"  ").getSingleResult(); 
        
        return rowCnt.intValue();
        
    }
    
    @Override
    public List<Seriespeliculas> consultarVideos(String idserie, String idvideo) {
        
        String condicion = "";
        
        if( idserie != null ){
            condicion += " and v.idseries.idseries = " + idserie + " ";
        }
        
        if( idvideo != null ){
            condicion += " and v.idpeliculas.idpeliculas = " + idvideo + " ";
        }
        
        Query query = em.createQuery( "SELECT v FROM Seriespeliculas v WHERE v.idseriespeliculas > 0 " + condicion + " " );
        
        return query.getResultList(); // or getSingleResult();
        
    }
    
}
